package com.project.communitymanagement.service.impl;

import com.project.communitymanagement.common.BaseServiceImpl;
import com.project.communitymanagement.entity.TbSalaryAdjustment;
import com.project.communitymanagement.entity.TbSalaryAdjustmentDts;
import com.project.communitymanagement.exception.BussinessException;
import com.project.communitymanagement.mapper.TbSalaryAdjustmentDtsMapper;
import com.project.communitymanagement.mapper.TbSalaryAdjustmentMapper;
import com.project.communitymanagement.service.TbSalaryAdjustmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TbSalaryAdjustmentServiceImpl  extends BaseServiceImpl<TbSalaryAdjustmentMapper, TbSalaryAdjustment> implements TbSalaryAdjustmentService {

    @Autowired
    private TbSalaryAdjustmentMapper tbsalaryadjustmentMapper;
    @Autowired
    private TbSalaryAdjustmentDtsMapper tbSalaryAdjustmentDtsMapper;

    @Override
    protected TbSalaryAdjustmentMapper mapper() {
        return tbsalaryadjustmentMapper;
    }

    @Override
    protected TbSalaryAdjustment defaultModel() {
        return new TbSalaryAdjustment();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(TbSalaryAdjustment tbSalaryAdjustment) {
        if(tbSalaryAdjustment.getId() == null || tbSalaryAdjustment.getId().equals("")){
            if(tbsalaryadjustmentMapper.getDataByUserId(tbSalaryAdjustment.getUser_id()) > 0)
                throw new BussinessException("当前用户已添加薪资，请进行调整薪资操作！");
        }
        int i = super.save(tbSalaryAdjustment);
        TbSalaryAdjustmentDts tbSalaryAdjustmentDts = new TbSalaryAdjustmentDts();
        tbSalaryAdjustmentDts.saveInit();
        tbSalaryAdjustmentDts.setUser_id(Integer.parseInt(tbSalaryAdjustment.getId()));
        tbSalaryAdjustmentDts.setSalary(tbSalaryAdjustment.getCurrent_salary());
        tbSalaryAdjustmentDtsMapper.insert(tbSalaryAdjustmentDts);
        return i;
    }
}